package test.chat.client.view;


import test.chat.client.websockets.ChatService;
import test.chat.common.command.LoginRequestCommand;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class LoginView extends Composite {
    MyUiBinder binder = GWT.create(MyUiBinder.class);

    interface MyUiBinder extends UiBinder<Widget, LoginView> {/*empty*/}

    @UiField
    Button button;

    @UiField
    TextBox nameBox;

    ChatService chatService;

    public LoginView(ChatService chatService) {
        initWidget(binder.createAndBindUi(this));

        this.chatService = chatService;
    }

    public HasText getNameBox() {
        return nameBox;
    }

    @UiHandler(value="button")
    public void onClick(@SuppressWarnings("unused") ClickEvent event) {
        if(!nameBox.getText().trim().isEmpty()) {
            LoginRequestCommand loginRequestCommand = new LoginRequestCommand(nameBox.getText());

            chatService.sendCommand(loginRequestCommand);
        } else {
            Window.alert("Введите имя!");
        }
    }
}
